package com.firefly.service.query.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.ums.po.*;
import com.firefly.dao.dao.ums.service.*;
import com.firefly.dao.assembler.ums.RoleAdminAssembler;
import com.firefly.dao.assembler.ums.RoleAssembler;
import com.firefly.dao.dto.ums.RoleQueryDto;
import com.firefly.dao.vo.ums.RoleAdminVo;
import com.firefly.dao.vo.ums.RoleListVo;
import com.firefly.dao.vo.ums.RolePermissionVo;
import com.firefly.dao.vo.ums.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 角色信息查询
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/5
 **/
@Service
public class RoleQuery {
    private final RoleService roleService;
    private final RolePermissionService rolePermissionService;
    private final MenuService menuService;
    private final RoleAdminService roleAdminService;
    private final AdminService adminService;
    private final RoleAssembler roleAssembler;
    private final RoleAdminAssembler roleAdminAssembler;
    @Autowired
    public RoleQuery(RoleService roleService, RolePermissionService rolePermissionService, MenuService menuService, RoleAdminService roleAdminService, AdminService adminService, RoleAssembler roleAssembler, RoleAdminAssembler roleAdminAssembler){
        this.roleService = roleService;
        this.rolePermissionService = rolePermissionService;
        this.menuService = menuService;
        this.roleAdminService = roleAdminService;
        this.adminService = adminService;
        this.roleAssembler = roleAssembler;
        this.roleAdminAssembler = roleAdminAssembler;
    }
    // 获取列表
    public RoleListVo getList(RoleQueryDto request) {
        QueryWrapper<RolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!request.key.isEmpty(), "name", request.key);
        Long count = roleService.count(queryWrapper);
        queryWrapper.last("limit " + request.offset + "," + request.pageSize);
        List<RolePo> all = roleService.list(queryWrapper);
        RoleListVo result = new RoleListVo();
        result.setCount(count);
        result.setList(roleAssembler.getListVo(all));
        return result;
    }

    public RoleVo getDetail(int id){
        RolePo detail = roleService.getById(id);
        return roleAssembler.getVo(detail);
    }

    // 获取权限信息
    public List<RolePermissionVo> getPriv(RoleQueryDto request){
        // 先检查角色权限
        QueryWrapper<RolePermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", request.detailId).select("id", "menu_id");
        List<RolePermissionPo> rp = rolePermissionService.list(queryWrapper);
        List<MenuPo> all = menuService.list(null);
        // 转格式
        List<RolePermissionVo> treeMenu = createTree(rp, all, 0);
        return treeMenu;
    }

    // 递归树形菜单
    private List<RolePermissionVo> createTree(List<RolePermissionPo> privs, List<MenuPo> menus, Integer pid) {
        List<RolePermissionVo> treeMenu = new ArrayList<>();
        for (MenuPo menu : menus) {
            if (menu.getParentId().equals(pid)) {
                RolePermissionVo tmp = new RolePermissionVo();
                tmp.setMenuId(menu.getId());
                tmp.setTitle(menu.getName());
                tmp.setExpand(false);
                tmp.setChecked(false);
                // 所有子菜单都选中的时候，此菜单选中checked，部分选中时selected
                List<Object> child_count = new ArrayList<>();
                child_count = menus.stream().filter((MenuPo m) -> Arrays.asList(m.getParentId()).contains(menu.getId())).collect(Collectors.toList());
                // 没有子菜单的时候，判断他本身
                if (child_count.size() == 0) {
                    child_count = privs.stream().filter((RolePermissionPo rpt) -> Arrays.asList(rpt.getMenuId()).contains(menu.getId())).collect(Collectors.toList());
                    boolean checked = child_count.size() > 0 ? true : false;
                    tmp.setChecked(checked);
                }
                List<RolePermissionVo> children = createTree(privs, menus, tmp.getMenuId());
                tmp.setChildren(children);
                treeMenu.add(tmp);
            }
        }
        return treeMenu;
    }

    // 获取所有用户
    public List<RoleAdminVo> getAdmins(RoleQueryDto request) {
        // 先检查角色权限
        QueryWrapper<RoleAdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", request.detailId).select("admin_id");
        List<Object> adminIds = roleAdminService.listObjs(queryWrapper);
        QueryWrapper<AdminPo> adminWrapper = new QueryWrapper<>();
        List<RoleAdminVo> list = new ArrayList<>();
        if (adminIds.size() > 0) {
            adminWrapper.in("id", adminIds);
            List<AdminPo> listAdmin = adminService.list(adminWrapper);
            list = roleAdminAssembler.getListVo(listAdmin);
        }
        return list;
    }
}
